package com.gugee.ins.data.task.config;

import com.gugee.ins.data.common.msg.*;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.LongSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.serializer.JsonSerializer;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author luolifeng
 * Date  2020/10/04
 */
@Configuration
@EnableKafka
public class KafkaProducerConfig {
    @Value("${kafka.producer.servers}")
    private String servers;
    @Value("${kafka.producer.retries}")
    private int retries;
    @Value("${kafka.producer.batch.size}")
    private int batchSize;
    @Value("${kafka.producer.linger.ms}")
    private int linger;
    @Value("${kafka.producer.buffer.memory}")
    private int bufferMemory;
    @Value("${kafka.producer.acks}")
    private String acks;


    public Map<String, Object> producerConfigs(Class  msgSerializer) {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
        props.put(ProducerConfig.RETRIES_CONFIG, retries);
        props.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
        props.put(ProducerConfig.LINGER_MS_CONFIG, linger);
        props.put(ProducerConfig.ACKS_CONFIG, acks);
        props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, LongSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, msgSerializer);
        return props;
    }

    @Bean("insBloggerMsgBaseInfoProducer")
    @ConditionalOnMissingBean
    public KafkaTemplate<Long, InsBloggerMessage> kafkaInsBloggerMessage() {
        return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(producerConfigs(InsBloggerMessage.MsgSerializer.class)));
    }

    @Bean("insBloggerIdenticalLevelMsgBaseInfoProducer")
    // @ConditionalOnMissingBean
    public KafkaTemplate<Long, InsMBIdenticalLevelMessage> kafkaInsMBIdenticalLevelMessage() {
        return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(producerConfigs(InsMBIdenticalLevelMessage.MsgSerializer.class)));
    }

    /**
     *
     * @return
     */
    @Bean("insBloggerMarkDayProducer")
    public KafkaTemplate<Long, InsMarkMessage> kafkaTemplateInsMarkDay() {
        return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(producerConfigs(InsMarkMessage.MsgSerializer.class)));
    }

    /**
     *
     * @return
     */
    @Bean("insBrandProducer")
    public KafkaTemplate<Long, InsBrandMessage> kafkaTemplateInsBrandMessage() {
        return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(producerConfigs(InsBrandMessage.MsgSerializer.class)));
    }

    /**
     *
     * @return
     */
    @Bean("kafkaTemplateMonitor")
    public KafkaTemplate<Long, EventMsg> kafkaTemplateMonitor() {
        return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(producerConfigs(JsonSerializer.class)));
    }

    /**
     *
     * @return
     */
    @Bean("insProductMsgProducer")
    public KafkaTemplate<Long, InsProductMessage> kafkaTemplateInsProductMsg() {
        return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(producerConfigs(InsProductMessage.MsgSerializer.class)));
    }
}
